<template>
	<div>
		<div class="box">
			<div class="">
				<FormItems
					ref="form"
					v-model="formData"
					:form="formList"
					:state="state"
					labelWidth="120px"></FormItems>
			</div>
		</div>
	</div>
</template>

<script>
	import FormItems from '@/views/finance/manage/formItems/index.vue';
	import FormItem from '@/views/finance/reimburs/formItem/index.vue';
	export default {
		components: { FormItems, FormItem },
		dicts: [
			'quota_added_services_type',
			'quota_insurance_buy_type',
			'in_su_rance_com_pany',
			'quota_insurance_standard_type',
		],
		props: {
			value: {
				required: false,
			},
			state: {
				required: false,
			},
			type: {
				required: false,
			},
		},
		inject: ['details_forecast', 'deptList', 'details_config', '_index'],

		data() {
			return {
				dicts: {
					insuranceBuyModeType: [],
					insuranceBuyModeType1: [
						{
							value: '1',
							label: '我司购买',
						},
					],
					insuranceBuyModeType2: [
						{
							value: '2',
							label: '店保',
						},
					],
					insuranceBuyModeType12: [
						{
							value: '1',
							label: '我司购买',
						},
						{
							value: '2',
							label: '店保',
						},
					],
					insuranceBuyModeType3: [
						{
							value: '3',
							label: '客户自购',
						},
					],
				},
			};
		},
		computed: {
			formData: {
				get() {
					return this.useVModel.call(this);
				},
				set(val) {
					this.$emit('input', val);
				},
			},
			details() {
				return this.details_forecast();
			},
			config() {
				return this.details_config();
			},
			index() {
				return this._index();
			},
			insuranceBuyTypeList() {
				let arr = [];
				let dict = this.dict.type.quota_insurance_buy_type;
				let list =
					this.config?.quotationProductSchemeAttributeOption
						?.insuranceOptions;
				if (list?.includes(1)) {
					if (dict?.[0]) {
						arr.push(dict?.[0]);
						this.dicts.insuranceBuyModeType.push(
							this.dicts.insuranceBuyModeType1,
						);
					}
				}
				if (list?.includes(2)) {
					if (dict?.[1]) {
						arr.push(dict?.[1]);
						this.dicts.insuranceBuyModeType.push(
							this.dicts.insuranceBuyModeType2,
						);
					}
				}
				if (dict?.[2]) {
					arr.push(dict?.[2]);
					this.dicts.insuranceBuyModeType.push(
						this.dicts.insuranceBuyModeType3,
					);
				}
				return arr;
			},
			formList() {
				return [
					{
						label: '购买类型',
						type: 'radioGroup',
						radioGroupList: this.insuranceBuyTypeList,
						prop: 'insuranceBuyType',
						rules: [
							{
								required: true,
								message: '请选择购买类型',
								trigger: 'change',
							},
						],
						event: {
							type: 'change',
							cb: row => {
								this.formData.insuranceBuyModeType = '';
								if (row.insuranceBuyType !== '2') {
									this.formData.maintenanceSupplierName =
										undefined;
								}
							},
						},
					},
					{
						label: '购买方式',
						type: 'radioGroup',
						radioGroupList:
							this.formData?.insuranceBuyType == '3'
								? this.dicts.insuranceBuyModeType3
								: this.formData?.insuranceBuyType == '1' ||
								  this.formData?.insuranceBuyType == '2'
								? this.dicts.insuranceBuyModeType12
								: this.insuranceBuyTypeList?.[0]?.value == '3'
								? this.dicts.insuranceBuyModeType3
								: [
										...this.dicts.insuranceBuyModeType12,
										...this.dicts.insuranceBuyModeType3,
								  ],
						prop: 'insuranceBuyModeType',
						rules: [
							{
								required: true,
								message: '请选择购买方式',
								trigger: 'change',
							},
						],
					},
					{
						label: '保险公司',
						type: 'select',
						selectList: this.dict.type.in_su_rance_com_pany,
						prop: 'insuranceCompanyKey',
						rules: true,
						event: {
							type: 'change',
							cb: row => {
								let item =
									this.dict.type.in_su_rance_com_pany.find(
										e => e.value == row.insuranceCompanyKey,
									);
								if (item) {
									this.formData.insuranceCompanyVal =
										item.label;
								}
							},
						},
					},
					// {
					//     label: "增值服务特约条款",
					//     type: "radioGroup",
					//     radioGroupList: this.dict.type.quota_added_services_type,
					//     prop: "addedServicesType",
					//     rules: [
					//         {
					//             required: true,
					//             message: "请选择增值服务特约条款",
					//             trigger: "change",
					//         },
					//     ],
					// },
					{
						label: '保险标准',
						type: 'radioGroup',
						radioGroupList:
							this.dict.type.quota_insurance_standard_type,
						prop: 'insuranceStandardType',
						rules: true,
						// event: {
						//     type: "change",
						//     cb: (row) => {
						//         this.$emit(
						//             "initDetaultInsuranceList",
						//             row.insuranceStandardType,
						//             true
						//         );
						//     },
						// },
					},
				];
			},
		},
		created() {},
		mounted() {
			// this.formData.insuranceStandardType ||= "4";
		},
		beforeDestroy() {
			if (this.notEmpty(this.details?.productList?.[this.index])) {
				this.$refs.form.form_resetFields();
				this.formData.insuranceBuyModeType = undefined;
				this.formData.insuranceBuyType = undefined;
				this.formData.insuranceCompanyKey = undefined;
				this.formData.insuranceCompanyVal = undefined;
				this.formData.insuranceStandardType = undefined;
			}
		},
		methods: {
			valid() {
				return new Promise(async resolve => {
					let result = true;
					let refs = Object.values(this.$refs);
					for (let index = 0; index < refs?.length; index++) {
						const ref = refs[index];
						if (
							Object.prototype.toString.call(ref) ==
							'[object Array]'
						) {
							for (let i = 0; i < ref.length; i++) {
								const element = ref[i];
								if (element?.valid) {
									let rc = await element?.valid?.();
									result = result && rc;
								}
								if (element?.validate) {
									let rf = await this._valid(ref);
									result = result && rf;
								}
								if (element?.form_validate) {
									let rf = await this._valid(
										ref,
										'form_validate',
									);
									result = result && rf;
								}
							}
						} else {
							if (ref?.valid) {
								let rc = await ref?.valid?.();
								result = result && rc;
							}
							if (ref?.validate) {
								let rf = await this._valid(ref);
								result = result && rf;
							}
							if (ref?.form_validate) {
								let rf = await this._valid(
									ref,
									'form_validate',
								);
								result = result && rf;
							}
						}
					}

					resolve(result);
				});
			},
		},
	};
</script>

<style lang="scss" scoped></style>
